using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace PP.Models
{ 
    public class CmsItemRepository : ICmsItemRepository
    {
        PPDbContext context = new PPDbContext();

        public IQueryable<CmsItem> All
        {
            get { return context.CmsItems; }
        }

        public IQueryable<CmsItem> AllIncluding(params Expression<Func<CmsItem, object>>[] includeProperties)
        {
            IQueryable<CmsItem> query = context.CmsItems;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public CmsItem Find(int id)
        {
            return context.CmsItems.Find(id);
        }

        public void InsertOrUpdate(CmsItem cmsitem)
        {
            if (cmsitem.Id == default(int)) {
                // New entity
                context.CmsItems.Add(cmsitem);
            } else {
                // Existing entity
                context.Entry(cmsitem).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var cmsitem = context.CmsItems.Find(id);
            context.CmsItems.Remove(cmsitem);
        }

        public void Save()
        {
            context.SaveChanges();
        }
    }

    public interface ICmsItemRepository
    {
        IQueryable<CmsItem> All { get; }
        IQueryable<CmsItem> AllIncluding(params Expression<Func<CmsItem, object>>[] includeProperties);
        CmsItem Find(int id);
        void InsertOrUpdate(CmsItem cmsitem);
        void Delete(int id);
        void Save();
    }
}